package com.mxy.myapplication2.java;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.mxy.myapplication2.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

public class ViewFlipperAndEventBusTest extends AppCompatActivity {

    private ViewFlipper viewFlipper;
    private ArrayList<String> names = new ArrayList<>(5);
    private ArrayList<String> phones = new ArrayList<>(5);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewflipper_eventbustest);
        initView();
        preDatas();
        initViewFlipper();
    }


    private void initView() {
        this.viewFlipper = findViewById(R.id.vf);
    }

    private void preDatas() {
        for (int i = 0; i < 5; i++) {
            this.names.add("马小勇");
            this.names.add("马小浩");
            this.names.add("苏小明");
            this.names.add("凌小宽");
            this.names.add("刘小菊");

            this.phones.add("123456");
            this.phones.add("321456");
            this.phones.add("789456");
            this.phones.add("321321");
            this.phones.add("654321");
        }
    }

    private void initViewFlipper() {

        for (int i = 0; i < 5; i++) {
            View itemView = getLayoutInflater().inflate(R.layout.item_viewflipper, null, false);
            itemView.setOnClickListener(new FlipperViewOnClickListener(names.get(i) + " 电话: " + phones.get(i)));
            TextView tvName = itemView.findViewById(R.id.tvName);
            TextView tvPhone = itemView.findViewById(R.id.tvPhone);
            tvName.setText(names.get(i));
            tvPhone.setText(phones.get(i));
            this.viewFlipper.addView(itemView);
        }
        this.viewFlipper.setAutoStart(true);
        this.viewFlipper.setFlipInterval(3 * 1000);
        this.viewFlipper.setInAnimation(this, R.anim.anim_viewflipper_inanim);
        this.viewFlipper.setOutAnimation(this, R.anim.anim_viewflipper_outanim);
    }

    @Override
    protected void onResume() {
        super.onResume();
        this.viewFlipper.startFlipping();
    }

    @Override
    protected void onPause() {
        super.onPause();
        this.viewFlipper.stopFlipping();
    }


    private class FlipperViewOnClickListener implements View.OnClickListener {

        private String toastContent;

        private FlipperViewOnClickListener(String toastContent) {
            this.toastContent = toastContent;
        }

        @Override
        public void onClick(View v) {
            Util.showToast(ViewFlipperAndEventBusTest.this, this.toastContent);
        }
    }


    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    /**
     * 响应方法必须带参 Object子类
     * 响应时根据发布者对象类型进行响应
     *
     * @param o
     */
    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void handlerMessage(Object o) {
        if (o instanceof Person) {
            System.out.println("person");
        }
        if (o instanceof Pojo) {
            System.out.println("pojo");
        }
        //受到粘性消息后进行处理，然后清除内存中粘性消息，节省资源
        EventBus.getDefault().removeAllStickyEvents();
    }
}